//
//  JKCommentListGetDataModel.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/12.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKCommentListGetDataModel: NSObject {
    
    /** 每次最多可获取评论数据数量 */
    static let maxCount = 20
    static var _longCmts = [JKCommentModel]()
    static var _shortCmts = [JKCommentModel]()
    
    /** 清除数据 */
    public class func clearData() {
        
        _longCmts.removeAll()
        _shortCmts.removeAll()
    }
    
    /** 加载 长评论 */
    public class func loadLongCommentsWithNewsID(newsID: String, finishedBlock:@escaping (_ models:  [JKCommentModel]?) -> ()) {
        
        JKNetworkingTool.shared.requestGet(url: "story/" + newsID + "/long-comments", parameters: nil) { (responseJson, error) in
            
            self.solveResponse(responseJson: responseJson, error: error, finishedBlock: finishedBlock)
        }
    }
    
    /**
     加载更多的长评论
     
     - parameter newsId:   文章的ID
     - parameter beforeId: 长评论的最后一个ID
     - parameter finished: 完成的回调
     */
    public class func loadMoreLongCommentsWithNewsID(newsID: String, beforeId: String, finishedBlock:@escaping (_ models:  [JKCommentModel]?) -> ()) {
        
        JKNetworkingTool.shared.requestGet(url: "story/" + newsID + "/long-comments/before/" + beforeId, parameters: nil) { (responseJson, error) in
            
            self.solveResponse(responseJson: responseJson, error: error, finishedBlock: finishedBlock)
        }
    }
    
    /** 加载 短评论 */
    public class func loadShortCommentsWithNewsID(newsID: String, finishedBlock:@escaping (_ models:  [JKCommentModel]?) -> ()) {
        
        JKNetworkingTool.shared.requestGet(url: "story/" + newsID + "/short-comments", parameters: nil) { (responseJson, error) in
            
            self.solveResponse(responseJson: responseJson, error: error, finishedBlock: finishedBlock)
        }
    }
    
    /** 加载更多的短评论 */
    public class func loadMoreShortCommentsWithNewsID(newsID: String, beforeID: String, finishedBlock:@escaping (_ models:  [JKCommentModel]?) -> ()) {
        
        JKNetworkingTool.shared.requestGet(url: "story/" + newsID + "/short-comments/before/" + beforeID, parameters: nil) { (responseJson, error) in
            
            self.solveResponse(responseJson: responseJson, error: error, finishedBlock: finishedBlock)
        }
    }
    
    /** 处理请求结果 */
    private class func solveResponse(responseJson: Any?, error: Error?, finishedBlock:@escaping (_ models:  [JKCommentModel]?) -> ()) {
        
        if error != nil {
            
            finishedBlock(nil)
            
            return
        }
        
        guard let json = responseJson as? [String : AnyObject] else {
            
            finishedBlock(nil)
            
            return
        }
        
        guard let comments = (json["comments"]) as? [[String : AnyObject]] else {
            
            finishedBlock(nil)
            
            return
        }
        
        guard let cmtModels = [JKCommentModel].deserialize(from: comments) else {
            
            finishedBlock(nil)
            
            return
        }
        
        finishedBlock(cmtModels as? [JKCommentModel])
    }
    
    /** 获取所有长评论数据 */
    public class func getAllLongCommentsWithNewsID(newsID: String, finishedBlock:@escaping (_ models:  [JKCommentModel]?) -> ()) {
        
        _longCmts.removeAll()
        
        loadLongCommentsWithNewsID(newsID: newsID) { (models) in
            
            if models == nil {
                
                return
            }
            
            _longCmts = models!
            
            // 数据不足20条，说明已经获取完毕，直接返回
            if models!.count < maxCount {
                
                finishedBlock(_longCmts)
                
                return
            }
            
            // 数据 == 20条，说明可能还有数据
            if models!.count == maxCount {
                
                getMoreLongCommentsWithNewsID(newsID: newsID, beforeID: _longCmts.last!.id!, finishedBlock: finishedBlock)
                
                return
            }
        }
    }
    
    /** 递归获取所有长评论数据 */
    private class func getMoreLongCommentsWithNewsID(newsID: String, beforeID: String, finishedBlock:@escaping (_ models:  [JKCommentModel]?) -> ()) {
        
        loadMoreLongCommentsWithNewsID(newsID: newsID, beforeId: beforeID) { (models) in
            
            if models == nil {
                
                finishedBlock(_longCmts)
                
                return
            }
            
            // 获取数据为0，说明已经获取完毕
            if (models!.count == 0) {
                
                finishedBlock(_longCmts);
                
                return
                
            }else if (models!.count < maxCount) {// 获取数据为小于20，说明已经获取完毕，添加到数组
                
                _longCmts += models!
                
                finishedBlock(_longCmts)
                
                return
                
            }else if (models!.count == maxCount) {// 数据 == 20条，说明可能还有数据
                
                _longCmts += models!
                
                getMoreLongCommentsWithNewsID(newsID: newsID, beforeID: _longCmts.last!.id!, finishedBlock: finishedBlock)
                
            }else{
                
                return
            }
            
        }
    }
    
    /** 获取所有短评论数据 */
    public class func getAllShortCommentsWithNewsID(newsID: String, finishedBlock:@escaping (_ models:  [JKCommentModel]?) -> ()) {
        
        _shortCmts.removeAll()
        
        loadShortCommentsWithNewsID(newsID: newsID) { (models) in
            
            if models == nil {
                
                return
            }
            
            _shortCmts = models!
            
            // 数据不足20条，说明已经获取完毕，直接返回
            if models!.count < maxCount {
                
                finishedBlock(_shortCmts)
                
                return
            }
            
            // 数据 == 20条，说明可能还有数据
            if models!.count == maxCount {
                
                getMoreShortCommentsWithNewsID(newsID: newsID, beforeID: _shortCmts.last!.id!, finishedBlock: finishedBlock)
                
                return
            }
        }
    }
    
    /** 递归获取所有短评论数据 */
    private class func getMoreShortCommentsWithNewsID(newsID: String, beforeID: String, finishedBlock:@escaping (_ models:  [JKCommentModel]?) -> ()) {
        
        loadMoreShortCommentsWithNewsID(newsID: newsID, beforeID: beforeID) { (models) in
            
            if models == nil {
                
                finishedBlock(_shortCmts)
                
                return
            }
            
            // 获取数据为0，说明已经获取完毕
            if (models!.count == 0) {
                
                finishedBlock(_shortCmts);
                
                return
                
            }else if (models!.count < maxCount) {// 获取数据为小于20，说明已经获取完毕，添加到数组
                
                _shortCmts += models!
                
                finishedBlock(_shortCmts)
                
                return
                
            }else if (models!.count == maxCount) {// 数据 == 20条，说明可能还有数据
                
                _shortCmts += models!
                
                getMoreShortCommentsWithNewsID(newsID: newsID, beforeID: _shortCmts.last!.id!, finishedBlock: finishedBlock)
                
            }else{
                
                return
            }
        }
    }
}
